
#include "DIALOG.h"

/*********************************************************************
*
*       Defines
*
**********************************************************************
*/
#define ID_WINDOW_0    (GUI_ID_USER + 0x05)
#define ID_MOVIE_0	   (GUI_ID_USER + 0x06)

#ifndef BOOT_ANIM_PIC_NUM
#define BOOT_ANIM_PIC_NUM    16
#endif
#ifndef BOOT_ANIM_TIME_MS
#define BOOT_ANIM_TIME_MS    300
#endif

#ifndef ARR_SIZE
#define ARR_SIZE(a)     (sizeof(a)/sizeof(a[0]))
#endif

/*********************************************************************
*
*       _aDialogCreate
*/
static const GUI_WIDGET_CREATE_INFO _aDialogCreate[] = {
  { WINDOW_CreateIndirect, "BootAnimDynamicWindow", ID_WINDOW_0, 0, 0, 800, 480, 0, 0x0, 0 },
  // USER START (Optionally insert additional widgets)
  // USER END
};

extern GUI_CONST_STORAGE GUI_BITMAP bmBootAnimStaticOpto;

extern GUI_CONST_STORAGE GUI_BITMAP bmBootAnim200x200_page_0003;
extern GUI_CONST_STORAGE GUI_BITMAP bmBootAnim200x200_page_0020;
extern GUI_CONST_STORAGE GUI_BITMAP bmBootAnim200x200_page_0024;

extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0001;
extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0002;
extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0003;
extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0004;
extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0005;
extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0006;
extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0007;
extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0008;
extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0009;
extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0010;
extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0011;
extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0012;
extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0013;
extern GUI_CONST_STORAGE GUI_BITMAP bmboot_page_0014;

const GUI_BITMAP *bootAnimArr[] = {
//	&bmboot_page_0001, 
		&bmboot_page_0002, &bmboot_page_0003, &bmboot_page_0004,
//	&bmboot_page_0005, &bmboot_page_0006, &bmboot_page_0007, &bmboot_page_0008, 
//	&bmboot_page_0009, &bmboot_page_0010, &bmboot_page_0011, &bmboot_page_0012,
//	&bmboot_page_0013, &bmboot_page_0013, &bmboot_page_0014, &bmboot_page_0014,
};

const GUI_BITMAP *bootAnimArr1[] = {
	&bmBootAnim200x200_page_0003, &bmBootAnim200x200_page_0020, &bmBootAnim200x200_page_0024,
};

/*********************************************************************
*
*       _cbDialog
*/
static void _cbDialog(WM_MESSAGE * pMsg) 
{
	static char index = 0;
	GUI_RECT Rect; 

  switch (pMsg->MsgId) 
  {
	case WM_INIT_DIALOG:
		
		break;
	case WM_PAINT:
		WM_GetClientRect(&Rect); 
#if   0
		GUI_DrawBitmap(bootAnimArr1[index], (Rect.x1-Rect.x0-320)/2, (Rect.y1-Rect.y0-240)/2);
#else
		GUI_DrawBitmap(&bmBootAnimStaticOpto, (Rect.x1-Rect.x0-200)/2, (Rect.y1-Rect.y0-200)/2-50);
	    GUI_DrawBitmap(bootAnimArr1[index], (Rect.x1-Rect.x0-200)/2, (Rect.y1-Rect.y0-200)/2);
#endif
		index = (index < (ARR_SIZE(bootAnimArr1)-1)) ? (index+1) : 0;
		break;
	case WM_TIMER:
//		if(!WM_IsCompletelyCovered(pMsg->hWin)) 
		WM_InvalidateWindow(pMsg->hWin);      /* Invalidate Window is used to redraw window, if not, picture will be static */      
		WM_RestartTimer(pMsg->Data.v, BOOT_ANIM_TIME_MS);
		break;
  default:
    WM_DefaultProc(pMsg);
    break;
  }
}

static GUI_MEMDEV_Handle hMem0; 
static WM_HWIN hMovie0;

static void _cbMove(WM_MESSAGE* pMsg)
{
	static char index = 0;
	GUI_RECT Rect; 

	switch(pMsg->MsgId) 
	{
	case WM_PAINT: 
		WM_GetClientRect(&Rect); 
#if   0
		GUI_DrawBitmap(bootAnimArr1[index], (Rect.x1-Rect.x0-320)/2, (Rect.y1-Rect.y0-240)/2);
#else
//		GUI_DrawBitmap(&bmBootAnimStaticOpto, (Rect.x1-Rect.x0-200)/2, (Rect.y1-Rect.y0-200)/2-50);
	    
	  hMem0 = GUI_MEMDEV_CreateEx((Rect.x1-Rect.x0-200)/2, (Rect.y1-Rect.y0-200)/2, 200, 200, GUI_MEMDEV_HASTRANS); 
	  GUI_MEMDEV_Select(hMem0); 
//	  GUI_DrawBitmap(&bmpic, 0, 0); 
	  GUI_DrawBitmap(bootAnimArr1[index], (Rect.x1-Rect.x0-200)/2, (Rect.y1-Rect.y0-200)/2);
	  GUI_MEMDEV_Select(0); 
#endif
		index = (index < (ARR_SIZE(bootAnimArr1)-1)) ? (index+1) : 0; 
		break;
	case WM_TIMER:
		if(!WM_IsCompletelyCovered(hMovie0)) 
			WM_InvalidateWindow(pMsg->hWin); 

		WM_RestartTimer(pMsg->Data.v, BOOT_ANIM_TIME_MS);
		break;
	default: WM_DefaultProc(pMsg); break;
	}
}

/*********************************************************************
*
*       Public code
*
**********************************************************************
*/
/*********************************************************************
*
*       CreateBootAnimDynamicWindow
*/
WM_HWIN App_CreateBootAnimDynamicWindow(void);
WM_HWIN App_CreateBootAnimDynamicWindow(void) 
{
  WM_HWIN hWin;

  hWin = GUI_CreateDialogBox(_aDialogCreate, GUI_COUNTOF(_aDialogCreate), _cbDialog, WM_HBKWIN, 0, 0);
  
  WM_CreateTimer(hWin, 0, BOOT_ANIM_TIME_MS, 0);

//  hMovie0 = WINDOW_CreateEx(300, 140, 200, 200, hWin, WM_CF_SHOW, 0, ID_MOVIE_0, _cbMove);
//  WM_CreateTimer(hMovie0, 0, BOOT_ANIM_TIME_MS, 0);
  
  return hWin;
}

// USER START (Optionally insert additional public code)
// USER END

/*************************** End of file ****************************/
